<?php
	/**
	 * CreateUser: zlh
	 * CreateTime: 2022/4/25 14:10
	 * Remark:
	 */

	namespace App\Func;

	use App\Consts\ResponseConsts\ResponseStatusConsts;
    use App\Models\Auth\RoleModel;
	use App\Models\Staff\cStaffRole;
	use App\Models\Staff\cStaffPermission;
    use App\Models\Staff\StaffModel;
    use Illuminate\Http\Exceptions\HttpResponseException;
    use Laravel\Passport\Token;

    class AuthFunc
	{
		// 权限
		private static string $AUTH				= 'AUTH';
		// 角色
		private static string $ROLE				= 'ROLE';
		// 公司
		private static string $COMPANY			= 'COMPANY';
		// 下一级公司
		private static string $NEXT_COMPANY		= 'NEXT_COMPANY';
		// 全部下级公司
		private static string $ALL_COMPANY		= 'ALL_COMPANY';

		/*  职员的权限
		 *
		 */
		public static function getCurrentStaffPermission()
		{
			# return (new cStaffPermission())->setCurrentStaffID(TokenFuc::getCurrentStaffInfo()['staff_id']);
			#
			return self::getCurrentStaffAuth();
		}

		/* 获取职员的全部权限融合不同角色下的全部权限
		 *
		 * tips :
		 *
		 *		1. call stack	=>	[ AuthFunc:getCurrentStaffAuth ] ---+ 
		 *																|
		 *																+---> [ AuthFunc::getDataByType(self::$AUTH) ] ---+ 
		 *																												  |
		 *																												  +---> [ self::getDisposeAuth(TokenFuc::getCurrentStaffInfo()) ] ---+
		 *																																													 |
		 *																																													 +---> [ (new cStaffRole())->setStaffInfo(TokenFuc::getCurrentStaffInfo())->getAllAuth() ]
		 *																																
		 *
		 *
		 *
		 */	
		public static function getCurrentStaffAuth()
		{
			return self::getDataByType(self::$AUTH);
		}

		/* 
		 *
		 */
		public static function getStaffDataBitPermissionByAuthCode($staff_info=null,$auth_code=null)
		{
			return (new cStaffRole())->setStaffInfo($staff_info==null?TokenFuc::getCurrentStaffInfo():$staff_info)->getStaffDataBitPermissionByAuthCode($auth_code); 
		}

		/* 获取包含此权限code的所有职员id
		 *
		 */
		public static function getAllStaffIDByAuthCode($authCode)
		{
			return (new cStaffRole())->getAllStaffIDByAuthCode($authCode);
		}

		/* 撤销角色id从到当前职员
		 * 
		 * by breezeer at 2022.04.27
		 *
		 * @parameter $ar_role          => array(1,2,3,)
		 * @parameter $ar_staff_info    => TokenFuc::getCurrentStaffInfo()
		 *
		 *
		 */
		public static function revokeRoleFromStaff($ar_role/* 撤销的角色id */,$ar_staff_info /* 职员 */)
		{
			return (new cStaffRole())->setStaffInfo($ar_staff_info)->revokeRoleFromStaff($ar_role);
		}
		
		/* 绑定角色id到当前职员
		 * 
		 * by breezeer at 2022.04.27
		 *
		 * @parameter $ar_role          => array(1,2,3,)
		 * @parameter $ar_staff_info    => TokenFuc::getCurrentStaffInfo()
		 *
		 *
		 */
		public static function addRoleToStaff($ar_role/* 绑定的角色id */,$ar_staff_info /* 职员 */)
		{
			return (new cStaffRole())->setStaffInfo($ar_staff_info)->addRoleToStaff($ar_role);
		}

		/* 获取当前职员的全部角色id
		 * 
		 * by breezeer at 2022.04.27
		 *
		 */
		public static function getCurrentStaffRole()
		{
			return self::getDataByType(self::$ROLE);
		}
		public static function getstaffOwnAllRole($staff_id)
		{
			return (new cStaffRole())->getstaffOwnAllRole(app()->gcHelper->toObjectEx(['staff_id'=>$staff_id]));
		}

		/* 获取当职员所在的公司
		 *
		 * 
		 */
		public static function getCurrentCompany()
		{
			return self::getDataByType(self::$COMPANY);
		}

		/* 获取当职员所在公司的下一级公司
		 *
		 * 
		 */
		public static function getCurrentNextCompany()
		{
			return self::getDataByType(self::$NEXT_COMPANY);
		}
		
		/* 获取所有的公司
		 *
		 */
		public static function getCurrentAllCompany()
		{
			return self::getDataByType(self::$ALL_COMPANY);
		}

		private static function getDataByType($type){

			# get current staff information
			#
			$currentInfo =  TokenFuc::getCurrentStaffInfo();
			
			#
			#
			switch ($type){
				case self::$COMPANY:{
					
					return self::getDisposeCompany($currentInfo);
				}
				case self::$ALL_COMPANY:{
					
					return self::getDisposeAllCompany($currentInfo);
				}
				case self::$NEXT_COMPANY:{
					
					return self::getDisposeNextCompany(self::getDisposeCompany($currentInfo));
				}
				case self::$AUTH:{
					
					return self::getDisposeAuth($currentInfo);
				}
				case self::$ROLE:{
					
					return self::getDisposeRole($currentInfo);
				}
			}
		}

		private static function getDisposeAuth($currentInfo){
			
			return (new cStaffRole())->setStaffInfo($currentInfo)->getAllAuth();
		}

		private static function getDisposeRole($currentInfo){
			
			return (new cStaffRole())->setStaffInfo($currentInfo)->getStaffAllRole();
		}

		private static function getDisposeCompany($currentInfo){
			
			return TokenFuc::getCurrentStaffInfo()['company_id'];
		}

		private static function getDisposeNextCompany($nCompanyId/* 当前的公司id */){
			
			return (new cStaffRole())->getNextCompany($nCompanyId);
		}
		
		private static function getDisposeAllCompany($currentInfo){
			
			return (new cStaffRole())->setStaffInfo($currentInfo)->getAllCompany();
		}

        public static function lackAuthResponse($data = []){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                $data,
                "缺少权限或非法访问",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

        public static function isSupperAdmin(){
            $staffTokenInfo = TokenFuc::getCurrentStaffInfo();
            if(!$staffTokenInfo){
                throw new HttpResponseException(ResponseFunc::returnJson(
                    'error',
                    null,
                    "缺少权限或非法访问",
                    ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                ));
            }
//            $staffInfo = (new StaffModel())->getStaffDetail($staffTokenInfo['staff_id']);
//            if($staffInfo['staff_name']!='cyhzroot'){
//                throw new HttpResponseException(ResponseFunc::returnJson(
//                    'error',
//                    null,
//                    "缺少权限或非法访问",
//                    ResponseStatusConsts::OK_200_RESPONSE_STATUS,
//                ));
//            }
        }
	}